repo: Add two more cap-std APIs
authorColin Walters <walters@verbum.org>
Thu, 3 Feb 2022 13:54:37 +0000 (08:54 -0500)
committerColin Walters <walters@verbum.org>
Fri, 6 May 2022 16:53:57 +0000 (12:53 -0400)
Followup to the previous PR.  I realized now with `io_lifetimes`
we can offer a safe `dfd_borrow()` that *borrows* the file descriptor
for the repository.  (In contrast to the current `.dfd()` that returns
the raw version)

Building on that, add another API that re-acquires a `Dir` instance.
(In the future in theory we could optimize this more by knowing
 whether or not the repo was constructed via cap-std, and perhaps
 in theory synthesize a `&Dir` reference, but I don't think we
 need that now)

rust-bindings/rust/src/repo.rs
rust-bindings/rust/tests/repo/mod.rs

index ec38ce8325d655de27ab6b3760e0fbff4b4433a8..147199c907f08e822e8024e203872e49ebf42ede 100644 (file)
@@ -145,6 +145,18 @@ impl Repo {
         }
     }
 
+    /// Borrow the directory file descriptor for this repository.
+    #[cfg(feature = "cap-std-apis")]
+    pub fn dfd_borrow<'a>(&'a self) -> io_lifetimes::BorrowedFd<'a> {
+        unsafe { io_lifetimes::BorrowedFd::borrow_raw_fd(self.dfd()) }
+    }
+
+    /// Return a new `cap-std` directory reference for this repository.
+    #[cfg(feature = "cap-std-apis")]
+    pub fn dfd_as_dir(&self) -> std::io::Result<cap_std::fs::Dir> {
+        cap_std::fs::Dir::reopen_dir(&self.dfd_borrow())
+    }
+
     /// Find all objects reachable from a commit.
     pub fn traverse_commit<P: IsA<gio::Cancellable>>(
         &self,
index 2cfde3db723adc9ecb4aac4c497b80233f972ce9..5d59aa5241373a1fe7824ccee87d329a3a81676d 100644 (file)
@@ -31,6 +31,10 @@ fn should_commit_content_to_repo_and_list_refs_again() {
 fn cap_std_commit() {
     let test_repo = CapTestRepo::new();
 
+    assert!(test_repo.dir.exists("config"));
+    // Also test re-acquiring a new dfd
+    assert!(test_repo.repo.dfd_as_dir().unwrap().exists("config"));
+
     assert!(test_repo.repo.require_rev("nosuchrev").is_err());
 
     let mtree = create_mtree(&test_repo.repo);